package com.ss.android.common.ui.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;

public class PinnedHeaderListView extends ListView implements OnScrollListener, OnItemSelectedListener {
    @TargetApi(value=11)
    static final class a {
        a(byte arg1) {
            this();
        }

        a() {

        }
    }

    final class b {
        View a;
        boolean b;
        int c;
        int d;
        int e;
        int f;
        boolean g;
        int h;
        int i;
        long j;

        b(byte arg1) {
            this();
        }

        private b() {

        }
    }

    public interface c {
        View a(ViewGroup arg1);

        void a(PinnedHeaderListView arg1);
    }

    b[] a;
    private PinnedHeaderListView.c b;
    private int c;
    private RectF d;
    private Rect e;
    private OnScrollListener f;
    private OnItemSelectedListener g;
    private int h;
    private int i;
    private boolean j;
    private long k;
    private int l;
    private int m;
    private boolean n;
    private boolean o;

    static {
        new a((byte) 0);
    }

    public PinnedHeaderListView(Context arg3) {
        this(arg3, null, 0);
    }

    public PinnedHeaderListView(Context arg2, AttributeSet arg3, int arg4) {
        super(arg2, arg3, arg4);
        this.d = new RectF();
        this.e = new Rect();
        this.i = 20;
        this.n = true;
        super.setOnScrollListener(((OnScrollListener)this));
        super.setOnItemSelectedListener(((OnItemSelectedListener)this));
    }

    public PinnedHeaderListView(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.d = new RectF();
        this.e = new Rect();
        this.i = 20;
        this.n = true;
        super.setOnScrollListener(((OnScrollListener)this));
        super.setOnItemSelectedListener(((OnItemSelectedListener)this));
    }

    public final void a() {
        b v0 = this.a[0];
        if(!v0.b || !v0.g || v0.f != 1) {
            v0.b = false;
        }
        else {
            v0.h = v0.c;
            if(!v0.g) {
                v0.b = true;
                v0.i = this.getBottom() + v0.d;
            }

            v0.g = true;
            v0.j = this.k;
        }
    }

    private void a(Canvas arg8, b arg9, long arg10) {
        if(arg9.g) {
            int v0 = ((int)(arg9.j - arg10));
            if(v0 <= 0) {
                arg9.c = arg9.i;
                arg9.b = false;
                arg9.g = false;
            }
            else {
                arg9.c = v0 * (arg9.h - arg9.i) / this.i + arg9.i;
            }
        }

        if(arg9.b) {
            View v0_1 = arg9.a;
            int v1 = arg8.save();
            arg8.translate(((float)this.l), ((float)arg9.c));
            if(arg9.f == 2) {
                this.d.set(0f, 0f, ((float)this.m), ((float)v0_1.getHeight()));
                arg8.saveLayerAlpha(this.d, arg9.e, 31);
            }

            v0_1.draw(arg8);
            arg8.restoreToCount(v1);
        }
    }

    final void b() {
        int v5 = 1073741824;
        View v1 = this.a[0].a;
        if(v1.isLayoutRequested()) {
            int v2 = View.MeasureSpec.makeMeasureSpec(this.m, v5);
            ViewGroup.LayoutParams v0 = v1.getLayoutParams();
            int v0_1 = v0 == null || v0.height <= 0 ? View.MeasureSpec.makeMeasureSpec(0, 0) : View.MeasureSpec.makeMeasureSpec(v0.height, v5);
            try {
                v1.measure(v2, v0_1);
            }
            catch(Exception v0_2) {
            }

            v0_1 = v1.getMeasuredHeight();
            this.a[0].d = v0_1;
            v1.layout(0, 0, this.m, v0_1);
        }
    }

    private void c() {
        int v0 = 0;
        this.j = false;
        while(v0 < this.c) {
            if(this.a[v0].g) {
                this.j = true;
                this.invalidate();
            }
            else {
                ++v0;
                continue;
            }

            return;
        }
    }

    protected void dispatchDraw(Canvas arg12) {
        int v10 = 2;
        int v3 = 0;
        long v0 = this.j ? System.currentTimeMillis() : 0;
        int v4 = 0;
        int v5 = this.getBottom();
        int v6 = 0;
        int v2;
        for(v2 = 0; v2 < this.c; ++v2) {
            b v8 = this.a[v2];
            if(v8.b) {
                if(v8.f == 1 && v8.c < v5) {
                    v5 = v8.c;
                    v4 = 1;
                }else if(v8.f == 0 || v8.f == v10) {
                    v4 = v8.c + v8.d;
                    if(v4 > v6) {
                        v6 = v4;
                        v4 = 1;
                    }else{
                        v4 = 1;
                    }
                }else {
                    v4 = 1;
                }
            }
        }

        if(!this.n) {
            v4 = 0;
        }

        if(v4 != 0 && !this.o) {
            arg12.save();
            this.e.set(0, v6, this.getWidth(), v5);
            arg12.clipRect(this.e);
        }

        super.dispatchDraw(arg12);
        if(v4 != 0) {
            arg12.restore();
            v2 = this.c;
            while(true) {
                --v2;
                if(v2 >= 0) {
                    b v4_1 = this.a[v2];
                    if(!v4_1.b) {
                        continue;
                    }

                    if(v4_1.f != 0 && v4_1.f != v10) {
                        continue;
                    }

                    this.a(arg12, v4_1, v0);
                    continue;
                }

                break;
            }

            while(v3 < this.c) {
                b v2_1 = this.a[v3];
                if((v2_1.b) && v2_1.f == 1) {
                    this.a(arg12, v2_1, v0);
                }

                ++v3;
            }
        }

        this.c();
    }

    public boolean getDrawPinnedHeader() {
        return this.n;
    }

    protected float getTopFadingEdgeStrength() {
        float v0 = this.c > 0 ? 0f : super.getTopFadingEdgeStrength();
        return v0;
    }

    public int getTotalTopPinnedHeaderHeight() {
        b v1;
        int v0 = this.c;
        do {
            --v0;
            if(v0 >= 0) {
                v1 = this.a[v0];
                if(!v1.b) {
                    continue;
                }

                if(v1.f != 0) {
                    continue;
                }

                break;
            }
            else {
                return 0;
            }
        }
        while(true);

        return v1.c + v1.d;
    }

    public boolean onInterceptTouchEvent(MotionEvent arg5) {
        boolean v0_1;
        if(this.h == 0) {
            int v1 = ((int)arg5.getY());
            int v0 = this.c;
            do {
                --v0;
                if(v0 >= 0) {
                    b v2 = this.a[v0];
                    if(!v2.b) {
                        continue;
                    }

                    if(v2.c > v1) {
                        continue;
                    }

                    if(v2.d + v2.c <= v1) {
                        continue;
                    }

                    break;
                }
                else {
                    return super.onInterceptTouchEvent(arg5);
                }
            }
            while(true);

            v0_1 = arg5.getAction() == 0 ? false : true;
        }
        else {
            v0_1 = super.onInterceptTouchEvent(arg5);
        }

        return v0_1;
    }

    public void onItemSelected(AdapterView arg7, View arg8, int arg9, long arg10) {
        int v0 = 0;
        int v2 = this.getHeight();
        int v1 = 0;
        while(true) {
            if(v0 < this.c) {
                b v3 = this.a[v0];
                if (v3.b) {
                    if (v3.f == 0) {
                        v1 = v3.c + v3.d;
                    } else if (v3.f == 1) {
                        v0 = v3.c;
                        break;
                    }
                }

                ++v0;
            }else{
                v0 = v2;
            }
        }
        View v2_1 = this.getSelectedView();
        if(v2_1 != null) {
            if(v2_1.getTop() < v1) {
                this.setSelectionFromTop(arg9, v1);
            }
            else if(v2_1.getBottom() > v0) {
                this.setSelectionFromTop(arg9, v0 - v2_1.getHeight());
            }
        }

        if(this.g != null) {
            this.g.onItemSelected(arg7, arg8, arg9, arg10);
        }
    }

    protected void onLayout(boolean arg3, int arg4, int arg5, int arg6, int arg7) {
        super.onLayout(arg3, arg4, arg5, arg6, arg7);
        this.l = this.getPaddingLeft();
        this.m = arg6 - arg4 - this.l - this.getPaddingRight();
    }

    public void onNothingSelected(AdapterView arg2) {
        if(this.g != null) {
            this.g.onNothingSelected(arg2);
        }
    }

    public void onScroll(AbsListView arg5, int arg6, int arg7, int arg8) {
        if(this.b != null) {
            if(1 != this.c) {
                this.c = 1;
                if(this.a == null) {
                    this.a = new b[this.c];
                }
                else if(this.a.length < this.c) {
                    b[] v0 = this.a;
                    this.a = new b[this.c];
                    System.arraycopy(v0, 0, this.a, 0, v0.length);
                }
            }

            int v0_1;
            for(v0_1 = 0; v0_1 < this.c; ++v0_1) {
                if(this.a[v0_1] == null) {
                    this.a[v0_1] = new b((byte) 0);
                }

                this.a[v0_1].a = this.b.a(((ViewGroup)this));
            }

            if(!this.n) {
            }else {
                this.k = System.currentTimeMillis() + (((long) this.i));
                this.b.a(this);
                this.c();
            }
        }

        if(this.f != null) {
            this.f.onScroll(((AbsListView)this), arg6, arg7, arg8);
        }
    }

    public void onScrollStateChanged(AbsListView arg2, int arg3) {
        this.h = arg3;
        if(this.f != null) {
            this.f.onScrollStateChanged(((AbsListView)this), arg3);
        }
    }

    public void setAdapter(ListAdapter arg2) {
        this.b = (c)arg2;
        super.setAdapter(arg2);
    }

    public void setDrawPinnedHeader(boolean arg1) {
        this.n = arg1;
    }

    public void setOnItemSelectedListener(OnItemSelectedListener arg1) {
        this.g = arg1;
        super.setOnItemSelectedListener(((OnItemSelectedListener)this));
    }

    public void setOnScrollListener(OnScrollListener arg1) {
        this.f = arg1;
        super.setOnScrollListener(((OnScrollListener)this));
    }

    public void setOverDrawHeader(boolean arg1) {
        this.o = arg1;
    }

    public void setPinnedHeaderAnimationDuration(int arg1) {
        this.i = arg1;
    }
}

